﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Globalization;
using System.Text.RegularExpressions;

namespace ImportStoresFromXML
{
    public class SalesImporter
    {
        public static void Main(string[] args)
        {
            string inputFile = "../../stores.xml";
            ProcessData(inputFile);
        }

        public static void ProcessData(string inputFile)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(inputFile);
            XmlNode salesReport = doc.DocumentElement;

            string dateString = salesReport.Attributes["date"].InnerText;
            string[] formats = { "d/MM/yyyy" };
            IFormatProvider formatProvider = null;
            DateTime date = DateTime.ParseExact(
                dateString, formats, formatProvider, 
                System.Globalization.DateTimeStyles.None);

            foreach (XmlNode store in salesReport.ChildNodes)
            {
                string storename = store.Attributes["store"].InnerText;
                Store DBstore = GetStore(storename);

                foreach (XmlNode sale in store.ChildNodes)
                {
                    string isbn = sale["isbn"].InnerText;
                    int amount = Int32.Parse(sale["amount"].InnerText);
                    Book DBbook = GetBook(isbn);
                    if (DBbook != null)
                    {
                        AddSale(DBstore, DBbook, date, amount);
                    }
                    else
                    {
                        Console.WriteLine("Warning: No book with ISBN {0} exists.", isbn);
                    }
                }
            }
        }

        public static Store GetStore(string storeName)
        {
            BookstoreDataContext context = new BookstoreDataContext();
            Store store;
            using (context)
            {
                List<Store> stores =
                (from s in context.Stores
                 where s.Name == storeName
                 select s).ToList();

                if (stores.Count > 0)
                {
                    store = stores[0];
                }
                else
                {
                    store = new Store();
                    {
                        store.Name = storeName;
                    };
                    context.Stores.InsertOnSubmit(store);
                    context.SubmitChanges();
                }
            }
            return store;
        }

        public static Book GetBook(string isbn)
        {
            isbn = isbn.Replace("-", String.Empty);
            BookstoreDataContext context = new BookstoreDataContext();
            using (context)
            {
                List<Book> books =
               (from b in context.Books
                where b.ISBN == isbn
                select b).ToList();
                if (books.Count == 0)
                {
                    return null;
                }
                else
                {
                    return books[0];
                }
            }
        }

        public static void AddSale(Store store, Book book, DateTime date, int amount)
        {
            BookstoreDataContext context = new BookstoreDataContext();
            using (context)
            {
                Sale sale = new Sale();
                {
                    sale.Amount = amount;
                    sale.Date = date;
                    sale.BookID = book.BookID;
                    sale.StoreID = store.StoreID;
                };
                context.Sales.InsertOnSubmit(sale);
                context.SubmitChanges();
            }
        }

    }
}
